import { _decorator, Component, Node,sp,Animation,resources,AnimationClip } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('SpineTs')
export class SpineTs extends Component {
    

    // 人物节点
    @property(Node)
    playerNode:Node = null;


    private skeleton1: sp.Skeleton = null;

    protected onLoad(): void {
        // 特别注意的是，在使用spine动画之前就必须初始化组件，比如需要在start上使用，就必须早start一步加载出来
        // 或者在使用前初始化组件
        // 初始化组件
        this.LoadAnimation()
    }
    start() {
    }

    playAnimation(name: string, loop: boolean) {
        this.skeleton1.setAnimation(0, name, loop);
    }
    // 提前加载动画
    LoadAnimation(){
        // 动态加载 AnimationClip
        resources.load('spine/mix-and-match-pro',  sp.SkeletonData, (err,SkeletonData) => {
            if (err) {
                console.error('动画资源加载失败', err);
                return;
            }else{
                console.log("动画资源加载成功")
                this.skeleton1 = this.playerNode.getComponent(sp.Skeleton);
                // this.skeleton.skeletonData = this.spineData;
                this.skeleton1.skeletonData = SkeletonData;
                // this.skeleton1.setAnimation(0, "blink", true);
                this.skeleton1.timeScale = 0;
            }
        });
    }
    
    // 行走动画
    walk(){
        if(this.skeleton1.timeScale == 0){
            this.skeleton1.setAnimation(0, "walk", true);
            this.skeleton1.timeScale = 1;
        }
        
    }
    // 站立动画
    blink(){
        if(this.skeleton1){
            this.skeleton1.setAnimation(0, "blink", true);
            this.skeleton1.timeScale = 0;
        }   
        
    }
}


